package itheima;

import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;

public class TreeSetDemo {
    public static void main(String[] args){
        //集合只能储存引出数据类型
          extracted();
//        text1();
          text2();
    }

    private static void extracted() {
        TreeSet<Integer> set1 = new TreeSet<>();
        set1.add(5);
        set1.add(4);
        set1.add(3);
        set1.add(2);
        set1.add(1);

        for (Integer integer : set1){
            System.out.println(integer);
        }
    }
    public static void text1(){
        TreeSet<Student> set =new TreeSet<>();

        set.add(new Student("罗欢",22));
        set.add(new Student("罗扬",20));
        set.add(new Student("罗燕妮",21));
        set.add(new Student("鲁伍加",19));
        for (Student student : set){
            System.out.println(student.getName() + "---"+ student.getAge());
        }
    }
    public static void text2(){
//        Comparator<Student> comparator = new Comparator<Student>() {
//            @Override
//            public int compare(Student o1, Student o2) {
//                //如果是正序排，则让o1-o2
//                //如果是倒序排，则让o2-o1
//                return 0;
//            }
//        };
        Comparator<Student> comparator=(o1,o2)-> o1.getAge()-o2.getAge();
        TreeSet<Student> set = new TreeSet<>(comparator);

        set.add(new Student("罗欢",22));
        set.add(new Student("罗扬",20));
        set.add(new Student("罗燕妮",21));
        set.add(new Student("鲁伍加",19));
        for (Student student : set){
            System.out.println(student.getName() + "---"+ student.getAge());
        }
    }
    public static int sum(int...a){
        int total = 0;
        for (int i=0;i<a.length;i++){
            total+=a[i];
        }
        return total;
    }

}
